package com.lej.my.balance

import android.text.Html
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseFragment
import com.lej.core.dsl.textRes
import com.lej.core.router.CRouter
import com.lej.core.utils.StringUtil
import com.lej.core.utils.extention.toast
import com.lej.core.utils.extention.versionName
import com.lej.dataservice.club.http.bean.onError
import com.lej.dataservice.club.http.bean.onSuccess
import com.lej.my.BR
import com.lej.my.R
import com.lej.my.databinding.MyFragmentBalanceBinding
import com.lej.theme.toast.CustomTips.showTips
import com.qmuiteam.qmui.kotlin.onClick

/**
 *  author: cjqCRouter
 *  date  : 2022/7/7 0007
 *  desc  : 余额提现页面
 */
@Route(path = CRouter.myBalance)
class MyBalanceFragment : BaseFragment<MyFragmentBalanceBinding, MyBalanceViewModel>() {
    override fun getLayout(): Int = R.layout.my_fragment_balance

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        viewModel.getBalanceResult()
        onClickListener()
        viewModel.getWithDrawRule(versionName = activity?.versionName ?: "").onSuccess(this) {
            val rule = it.get("withdrawRule").asString
            binding.tvRuleTipsContent.text = Html.fromHtml(rule)
        }
    }

    private fun onClickListener() {
        binding.ivTips.onClick {  //余额页提示
            viewModel.pageState.value = page_tips
        }
        binding.tvDeposit.onClick { //余额页提现
            if (viewModel.balanceNum < 100f) {
                binding.tvErrorTipsContent.textRes = R.string.my_balance_error_enough
                viewModel.pageState.value = page_error
                return@onClick
            }
            binding.etInputNum.hint = StringUtil.getString(
                R.string.my_balance_withdrawal_hint_num,
                (viewModel.balanceNum / 100f).toString()
            )
            viewModel.pageState.value = page_input
        }
        binding.tvRuleTipsKnow.onClick {  //规则页我知道了
            viewModel.pageState.value = page_withdrawal
        }
        binding.tvInputConfirm.onClick {  //填银行信息页确定
            withdrawConfirm()
        }
        binding.ivInputClose.onClick { //填银行信息页关闭
            viewModel.pageState.value = page_withdrawal
            clearText()
        }
        binding.tvErrorTipsKnow.onClick { //错误提示我知道了
            viewModel.pageState.value = page_withdrawal
        }
    }

    private fun withdrawConfirm() {
        if (binding.etInputNum.text.isNullOrEmpty()) {
            "请输入提现金额".toast()
            return
        }
        if (binding.etInputNum.text.toString().toFloat() > viewModel.balanceNum / 100f) {
            "提现金额超过余额".toast()
            return
        }
        if (binding.etInputName.text.isNullOrEmpty()) {
            "请输入银行开户人名称".toast()
            return
        }
        if (binding.etInputBank.text.isNullOrEmpty()) {
            "请输入银行名称".toast()
            return
        }
        if (binding.etInputCard.text.isNullOrEmpty()) {
            "请输入银行账号".toast()
            return
        }
        viewModel.withDraw(
            (binding.etInputNum.text.toString().toFloat() * 100).toString(),
            binding.etInputName.text.toString(),
            binding.etInputCard.text.toString(),
            binding.etInputBank.text.toString()
        ).onSuccess(this) {
            context?.showTips("提现申请成功")
            viewModel.getBalanceResult()
            viewModel.pageState.value = page_withdrawal
            clearText()
        }.onError(this) {
            binding.tvErrorTipsContent.text = it.message
            viewModel.pageState.value = page_error
        }
    }

    private fun clearText() {
        binding.etInputNum.text?.clear()
        binding.etInputName.text?.clear()
        binding.etInputCard.text?.clear()
        binding.etInputBank.text?.clear()
    }

}